package cn.yshujia.service.impl;

import cn.yshujia.domain.entity.Role;
import cn.yshujia.domain.enums.PermissionEnum;
import cn.yshujia.mapper.RoleMapper;
import cn.yshujia.repository.RoleRepository;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author YShuJia
 * @create 2024/4/23
 * @description 服务层
 */

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IService<Role> {
	
	@Resource
	RoleMapper mapper;
	
	@Resource
	RoleRepository repository;
	
	public Role selectById(Long id) {
		return repository.getRole(id);
	}
	
	public List<Role> list() {
		return repository.list();
	}
	
	public Map<String, List<String>> mapPermission() {
		Map<String, List<String>> map = new HashMap<>();
		List<String> list = new ArrayList<>();
		list.add(PermissionEnum.ALBUM_ALL.getValue());
		list.add(PermissionEnum.ALBUM_INSERT.getValue());
		list.add(PermissionEnum.ALBUM_DELETE.getValue());
		list.add(PermissionEnum.ALBUM_UPDATE.getValue());
		map.put("album", list);
		list = new ArrayList<>();
		list.add(PermissionEnum.USER_ALL.getValue());
		list.add(PermissionEnum.USER_INSERT.getValue());
		list.add(PermissionEnum.USER_DELETE.getValue());
		list.add(PermissionEnum.USER_UPDATE.getValue());
		map.put("user", list);
		list = new ArrayList<>();
		list.add(PermissionEnum.ROLE_ALL.getValue());
		list.add(PermissionEnum.ROLE_INSERT.getValue());
		list.add(PermissionEnum.ROLE_DELETE.getValue());
		list.add(PermissionEnum.ROLE_UPDATE.getValue());
		map.put("role", list);
		list = new ArrayList<>();
		list.add(PermissionEnum.CLASSIFY_ALL.getValue());
		list.add(PermissionEnum.CLASSIFY_INSERT.getValue());
		list.add(PermissionEnum.CLASSIFY_DELETE.getValue());
		list.add(PermissionEnum.CLASSIFY_UPDATE.getValue());
		map.put("classify", list);
		list = new ArrayList<>();
		list.add(PermissionEnum.LABEL_ALL.getValue());
		list.add(PermissionEnum.LABEL_INSERT.getValue());
		list.add(PermissionEnum.LABEL_DELETE.getValue());
		list.add(PermissionEnum.LABEL_UPDATE.getValue());
		map.put("label", list);
		list = new ArrayList<>();
		list.add(PermissionEnum.COMMENT_ALL.getValue());
		list.add(PermissionEnum.COMMENT_INSERT.getValue());
		list.add(PermissionEnum.COMMENT_DELETE.getValue());
		list.add(PermissionEnum.COMMENT_UPDATE.getValue());
		map.put("comment", list);
		list = new ArrayList<>();
		list.add(PermissionEnum.RESOURCES_ALL.getValue());
		list.add(PermissionEnum.RESOURCES_INSERT.getValue());
		list.add(PermissionEnum.RESOURCES_DELETE.getValue());
		list.add(PermissionEnum.RESOURCES_UPDATE.getValue());
		map.put("resources", list);
		list = new ArrayList<>();
		list.add(PermissionEnum.ARTICLE_ALL.getValue());
		list.add(PermissionEnum.ARTICLE_INSERT.getValue());
		list.add(PermissionEnum.ARTICLE_DELETE.getValue());
		list.add(PermissionEnum.ARTICLE_UPDATE.getValue());
		map.put("article", list);
		list = new ArrayList<>();
		list.add(PermissionEnum.LIKE_ALL.getValue());
		list.add(PermissionEnum.LIKE_INSERT.getValue());
		list.add(PermissionEnum.LIKE_DELETE.getValue());
		list.add(PermissionEnum.LIKE_UPDATE.getValue());
		map.put("like", list);
		list = new ArrayList<>();
		list.add(PermissionEnum.WEBSITE_ALL.getValue());
		list.add(PermissionEnum.WEBSITE_INSERT.getValue());
		list.add(PermissionEnum.WEBSITE_DELETE.getValue());
		list.add(PermissionEnum.WEBSITE_UPDATE.getValue());
		map.put("website", list);
		list = new ArrayList<>();
		list.add(PermissionEnum.TREE_HOLE_ALL.getValue());
		list.add(PermissionEnum.TREE_HOLE_INSERT.getValue());
		list.add(PermissionEnum.TREE_HOLE_DELETE.getValue());
		list.add(PermissionEnum.TREE_HOLE_UPDATE.getValue());
		map.put("tree_hole", list);
		list = new ArrayList<>();
		list.add(PermissionEnum.ROUTER_ALL.getValue());
		list.add(PermissionEnum.ROUTER_INSERT.getValue());
		list.add(PermissionEnum.ROUTER_DELETE.getValue());
		list.add(PermissionEnum.ROUTER_UPDATE.getValue());
		map.put("router", list);
		return map;
	}
	
	@Transactional (rollbackFor = Exception.class)
	public Boolean update(Role role) {
		int result = mapper.updateById(role);
		if (result > 0) {
			repository.del();
		}
		return result > 0;
	}
}